package com.zh.rent.controller.backstage;

import com.github.pagehelper.PageInfo;
import com.zh.rent.pojo.Account;
import com.zh.rent.pojo.MailContent;
import com.zh.rent.pojo.Setup;
import com.zh.rent.pojo.StandbyAccount;
import com.zh.rent.service.AccountService;
import com.zh.rent.service.MailContentService;
import com.zh.rent.service.SetupService;
import com.zh.rent.service.StanAccountService;
import com.zh.rent.util.EmailUtil;
import com.zh.rent.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/mail")
@Api(tags="后台验证码")
public class MailContentController {
    @Autowired
    MailContentService MailContentServerImpl;
    @Autowired
    AccountService AccountServiceImpl;
    @Autowired
    StanAccountService StanAccountServiceImpl;
    @Autowired
    SetupService SetupServiceImpl;

    @GetMapping("/mailAll")
    @ResponseBody
    @ApiOperation(value = "验证码显示")
    public JsonResult mailAll(@RequestParam(value="pageSize",defaultValue="5")Integer pageSize,
                                      @RequestParam(value="pageNum",defaultValue="1")Integer pageNum,
                                      String name) {
        Map<String,Object> map = new HashMap<>();
        name = "%"+name+"%";
        map.put("name",name);
       /* List<Account> accounts = AccountServiceImpl.selAccAll();//查询所有的账号
        Integer accid=null;
        String accName=null;
        String accType=null;
        String MailboxName=null;
        String Authorization=null;
        String Host=null;
        List<MailContent> list = new ArrayList<>();*/
        PageInfo<MailContent> mailContentPageInfo=null;
      /*  if(accounts!=null){
            for (Account account : accounts) {
                System.out.println("备用账号id："+account.getStandbyid()==null? true:false+";;;"+account.getStandbyid());
                if(account.getStandbyid()==null){
                    System.out.println("账号");
                     accid=account.getAccountid();//账号id
                    accName=account.getAccountName();//账号
                    accType=account.getAccountType();//类型
                    MailboxName=account.getMailboxName();//邮箱
                    Authorization=account.getAuthorization();//授权码
                    Host=account.getHost();//服务器
                }else {
                    System.out.println("备用账号");
                    StandbyAccount standbyAccount = StanAccountServiceImpl.selStanAcc(account.getStandbyid());
                    accid=standbyAccount.getStandbyid();//账号id
                    accName=standbyAccount.getAccountName();//账号
                    accType=standbyAccount.getAccountType();//类型
                    MailboxName=standbyAccount.getMailboxName();//邮箱
                    Authorization=standbyAccount.getAuthorization();//授权码
                    Host=standbyAccount.getHost();//服务器
                }
                List<Map<String, Object>> emali=null;
                try {
                    emali = EmailUtil.Emali(MailboxName, Authorization, Host);
                } catch (Exception e) {
                   return JsonResult.ERROR("错误");
                }
                if(emali!=null){
                    for (Map<String, Object> strMap : emali) {
                        MailContent mailContent = new MailContent();
                        mailContent.setAccountid(accid);
                        mailContent.setAccountName(accName);
                        mailContent.setAccountType(accType);
                        mailContent.setMailboxName(MailboxName);
                        mailContent.setMailTime((String) strMap.get("Ttime"));
                        mailContent.setMaliText((String) strMap.get("txtcontent"));
                        mailContent.setSite((String) strMap.get("site"));
                        mailContent.setMalicode((String) strMap.get("code"));
                        list.add(mailContent);
                    }
                }

            }
            int i = MailContentServerImpl.updateMail(list);//添加进数据库-

        }*/
        mailContentPageInfo = MailContentServerImpl.mailconAll(pageNum,pageSize,map);
        return JsonResult.OK(mailContentPageInfo);
    }

    @GetMapping("/mail")
    @ResponseBody
    @ApiOperation(value = "验证码")
    public JsonResult mail(){
        Setup setup = SetupServiceImpl.setsetupAll();
        List<MailContent> list = new ArrayList<>();
        List<Map<String, Object>> emali = null;
        try {
            emali = EmailUtil.Emali(setup.getReserved2(), setup.getReserved3(), setup.getReserved4());
            //Future<String> stringFuture = asyncTaskService.doTaskTwo(MailboxName, Authorization, Host, accid, accName, accType);
            if (emali != null) {
                for (Map<String, Object> strMap : emali) {
                    MailContent mailContent = new MailContent();
                    //  mailContent.setAccountid(accid);
                    mailContent.setAccountName((String) strMap.get("accName"));
                    //  mailContent.setAccountType(accType);
                    String s=(String) strMap.get("MailboxName");
                    String substring = s.substring(1, s.length()-1);
                    mailContent.setMailboxName(substring);
                    mailContent.setMailTime((String) strMap.get("Ttime"));
                    mailContent.setMaliText((String) strMap.get("txtcontent"));
                    mailContent.setSite((String) strMap.get("site"));
                    mailContent.setMalicode((String) strMap.get("code"));
                    System.out.println("获取获取3333:"+strMap.get("accName"));
                    list.add(mailContent);
                }
            }
            System.out.println("获取获取11111:"+list.toString());

            if(list!=null){
                int i = MailContentServerImpl.updateMail(list);//添加进数据库
            }
        } catch (Exception e) {
            System.out.println("验证码存数据库出错了：" + e.getMessage());
        }
        return JsonResult.OK();
    }

}
